package com.aistar.service.redis.impl;



import com.aistar.mapper.TravPraiseMapper;
import com.aistar.mapper.TravelnoteMapper;
import com.aistar.pojo.TravPraise;

import com.aistar.pojo.Travelnote;
import com.aistar.service.redis.RedisService;
import com.aistar.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    TravelnoteMapper travelnoteMapper;
    @Autowired
    RedisService redisService;
    @Autowired
    TravPraiseMapper travPraiseMapper;



    @Override
    public TravPraise selectStatuspraiseTravIdByAndUserId(Integer praiseTravId, Integer praiseUserId) {
        return travPraiseMapper.selectByPrimaryKey(praiseTravId,praiseUserId);
    }

    @Override
    public void saveLiked(String likedUserId, String likedInfoId) {
        String key = RedisKeyUtil.getLikedKey(likedUserId, likedInfoId);
        redisTemplate.opsForHash().put("MAP_KEY_USER_LIKED_UP", key, "1");

    }

    @Override
    public void unLiked(String likedUserId, String likedInfoId) {
        String key = RedisKeyUtil.getLikedKey(likedUserId, likedInfoId);
        redisTemplate.opsForHash().put("MAP_KEY_USER_LIKED_UP", key, "0");
    }

    @Override
    public int selectCuntByInfoId(String infoId) {
        Object count = redisTemplate.opsForHash().get("MAP_KEY_USER_LIKED_COUNT_UP",infoId);
        if(count==null)
            return 0;
        return (int) count;
    }

    @Override
    public Integer selectStatusByInfoIdAndUserId(String praiseTravId, String praiseUserId) {
        String key = RedisKeyUtil.getLikedKey(praiseUserId, praiseTravId);
        Object status = redisTemplate.opsForHash().get("MAP_KEY_USER_LIKED_UP",key);
        return Integer.valueOf(String.valueOf(status));
    }

    @Override
    public void deleteLiked(String likedUserId, String likedInfoId) {
        String key = RedisKeyUtil.getLikedKey(likedUserId, likedInfoId);
        redisTemplate.opsForHash().delete("MAP_KEY_USER_LIKED_UP", key);
    }

    @Override
    public void insertLikedCount(String likedInfoId) {
        redisTemplate.opsForHash().increment("MAP_KEY_USER_LIKED_COUNT_UP", likedInfoId, 1);
    }

    @Override
    public void deleteLikedCount(String likedInfoId) {
        redisTemplate.opsForHash().increment("MAP_KEY_USER_LIKED_COUNT_UP", likedInfoId, -1);
    }

    @Override
    public List<TravPraise> getLikedDataFromRedis() {
        Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("MAP_KEY_USER_LIKED_UP", ScanOptions.NONE);
        List<TravPraise> travPraises = new ArrayList<>();
        while(cursor.hasNext()){
            Map.Entry<Object, Object> entry = cursor.next();
            String key = String.valueOf(entry.getKey());
            //分离出 likedUserId，likedPostId
            String[] split = key.split("::");
            Integer UId = Integer.valueOf(split[0]);
            System.out.println("split[0]:"+split[0]);
            Integer TId = Integer.valueOf(split[1]);
            System.out.println("split[1]:"+split[1]);
            Integer value = Integer.parseInt(String.valueOf(entry.getValue()));

            //组装成 UserLike 对象
            TravPraise travPraise = new TravPraise(TId,UId,value);
            travPraises.add(travPraise);

            //存到 list 后从 Redis 中删除
            //redisTemplate.opsForHash().delete("MAP_KEY_USER_LIKED",key);
        }
        System.out.println("travPraises:"+travPraises);
        return travPraises;
    }
    // 获得所有的点赞数量
    @Override
    public List<Travelnote> getLikedCountFromRedis() {
        Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("MAP_KEY_USER_LIKED_UP", ScanOptions.NONE);

        List<Travelnote> travelnoteArrayList= new ArrayList<>();
        while(cursor.hasNext()){
            Map.Entry<Object, Object> map = cursor.next();
            System.out.println("cursor:" + map.toString());
            //将点赞数量存储在 LikedCountDT
            String key = (String)map.getKey();
            System.out.println("key:"+key);
            String id[] = key.split("::");
            System.out.println("id[1]:"+id[1]);
            if(Integer.parseInt(String.valueOf(map.getValue())) == 1){
                Travelnote dto = new Travelnote(Integer.parseInt(id[1]),  Integer.parseInt(String.valueOf(map.getValue())));
                travelnoteArrayList.add(dto);
            }else{
                Travelnote dto = new Travelnote(Integer.parseInt(id[1]), -1);
                travelnoteArrayList.add(dto);
            }
            System.out.println("靠靠靠靠靠靠靠靠靠靠靠靠靠靠靠靠靠靠靠" + Integer.parseInt(String.valueOf(map.getValue())));
            //从Redis中删除这条记录
           redisTemplate.opsForHash().delete("MAP_KEY_USER_LIKED_UP",key);
        }
        System.out.println("Redis数据量：" + travelnoteArrayList.size());
        return travelnoteArrayList;
    }

    @Override
    public Integer selectNumBytravId(Integer travid) {
        return travelnoteMapper.selectPraiseBytravid(travid);
    }
}
